package article.dao.impl;

import article.dao.ArticleDao;
import article.entity.ArticleVO;
import generic.dao.impl.GenericDaoImpl;
import generic.page.Pages;

import java.util.List;

/**
 * 文章的crud
 */
public class ArticleDaoImpl extends GenericDaoImpl<ArticleVO,Integer> implements ArticleDao{
    @Override
    public ArticleVO findArticleVOById(Integer articleId) {

        return (ArticleVO)this.findById(ArticleVO.class,articleId);
    }

    @Override
    public void deleteArticleById(Integer articleId) {
        this.delete(findArticleVOById(articleId));

    }

    @Override
    public ArticleVO updateArticle(ArticleVO articleVO) {
        this.update(articleVO);
        return articleVO;
    }

    @Override
    public ArticleVO addArticleVO(ArticleVO articleVO) {
        this.save(articleVO);
        return articleVO;
    }

    @Override
    public Pages queryPageByCategory(Pages pages,Integer categoryId) {

        StringBuffer hql2= new StringBuffer();
        hql2.append("select count(*) from ArticleVO a where a.categoryVO.id=").append(categoryId);
        pages.setRowCount(Integer.parseInt(this.getSession().createQuery(hql2.toString()).uniqueResult().toString()));

        Pages.init(pages);

        //查询记录
        hql2.delete(0,15);
        List list = this.getSession().createQuery(hql2.toString())
                .setFirstResult(pages.getOffset())
                .setMaxResults(pages.getPageSize()).list();
        pages.setList(list);
        return pages;
    }

    @Override
    public Pages queryPageByBlogData(Pages pages,Integer blogDataId) {

        StringBuffer hql2= new StringBuffer();
        hql2.append("select count(*) from ArticleVO a where a.blogDataVO.blogDataId=").append(blogDataId);
        pages.setRowCount(Integer.parseInt(this.getSession().createQuery(hql2.toString()).uniqueResult().toString()));

        Pages.init(pages);

        //查询记录
        hql2.delete(0,15);
        List list = this.getSession().createQuery(hql2.toString())
                .setFirstResult(pages.getOffset())
                .setMaxResults(pages.getPageSize()).list();
        pages.setList(list);
        return pages;
    }
}
